﻿using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;

namespace ZYing.LiquidThemeEngine.AspNetCore
{
    public class LiquidThemeViewResult : IActionResult, IStatusCodeActionResult
    {
        public LiquidThemeTemplateContext TemplateContext { get; }
        public string View { get; }
        public LiquidThemeViewResult(LiquidThemeTemplateContext templateContext, string view)
        {
            TemplateContext = templateContext ?? throw new ArgumentNullException(nameof(templateContext));
            View = !string.IsNullOrWhiteSpace(view) ? view : throw new ArgumentNullException(nameof(view));
        }

        public async Task ExecuteResultAsync(ActionContext context)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));

            var executor = context.HttpContext.RequestServices
                .GetRequiredService<IActionResultExecutor<LiquidThemeViewResult>>();
            await executor.ExecuteAsync(context, this);
        }

        public int? StatusCode { get; set; }
    }
}